//package com.link.play.fit.ai.utils
//
//import com.android.installreferrer.api.InstallReferrerClient
//import com.android.installreferrer.api.InstallReferrerStateListener
//import com.link.play.fit.ai.kv.AuthCache
//import kotlinx.coroutines.CoroutineExceptionHandler
//import kotlinx.coroutines.Dispatchers
//import kotlinx.coroutines.MainScope
//import kotlinx.coroutines.launch
//import org.json.JSONObject
//
//object InstallReferrerHelps {
//
//    fun init() {
//        MainScope().launch(CoroutineExceptionHandler { _, _ -> } + Dispatchers.IO) {
//            updateInstallReferrer()
//        }
//    }
//
//    private fun updateInstallReferrer() {
//        val referrerClient = InstallReferrerClient.newBuilder(AppX.context).build()
//        referrerClient.startConnection(object : InstallReferrerStateListener {
//            override fun onInstallReferrerSetupFinished(responseCode: Int) {
//                try {
//                    when (responseCode) {
//                        InstallReferrerClient.InstallReferrerResponse.OK -> {
//                            val response = referrerClient.installReferrer
//                            val installInfo = response.installReferrer
//                            val obj = JSONObject()
//                            obj.put("referrerUrl", response.installReferrer)
//                            obj.put("referrerClickTime", response.referrerClickTimestampSeconds)
//                            obj.put("appInstallTime", response.installBeginTimestampSeconds)
//                            obj.put("instantExperienceLaunched", installInfo.toString())
//                            AuthCache.installReferrer = response.installReferrer
//                            //EventUtils.event("install_referrer", ext = obj)
//                            if (listOf(
//                                    "gclid",
//                                    "facebook",
//                                    "instagram"
//                                ).all { !installInfo.contains(it, true) }
//                            ) {
//                                //自然用户
//                                // SpUtils.getInstance().put("install_source", "origin")
//                            } else {
//                                //渠道用户
//                                // SpUtils.getInstance().put("install_source", "channel")
//                            }
//                        }
//
//                        else -> {
//                            // EventUtils.event("install_referrer_error")
//                        }
//                    }
//                    getHttpConfig()
//                } catch (_: Exception) {
//                    //EventUtils.event("install_referrer_error")
//                    getHttpConfig()
//                }
//            }
//
//            override fun onInstallReferrerServiceDisconnected() {
//            }
//
//        })
//    }
//
//    private fun getHttpConfig() {
//        ConfigUtils.requestConfig {
//        }
//    }
//}